package com.wn.servlet;

import com.alibaba.fastjson.JSON;
import com.wn.bean.Owner;
import com.wn.service.OwnerService;
import com.wn.service.impl.OwnerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 *   javaEE 的标准 (接口)
 *    Servlet 的标准是一堆接口
 *         Servlet 底层是一堆的接口
 *            init      初始化
 *            service   提供服务的方法 (每次请求都会执行)
 *            destroy   销毁
 *        GenericServlet 只需要重写 一个
 *            service 方法就可以 处理请求
 *
 *        HttpServlet   专门处理HTTP请求的servlet
 *            service()  处理http请求的
 *        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
 *         HttpServletRequest request;
 *         HttpServletResponse response;
 *         try {
 *             request = (HttpServletRequest)req;
 *             response = (HttpServletResponse)res;
 *         } catch (ClassCastException var6) {
 *             throw new ServletException(lStrings.getString("http.non_http"));
 *         }
// *         this.service(request, response);
 *     }
 *          service(request, response)  (这个service 进行 请求方式的分发)
 *
 *       protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 *         String method = req.getMethod();
 *         long lastModified;
 *         if (method.equals("GET")) {
 *              this.doGet(req, resp);
 *         }else if (method.equals("POST")) {
 *             this.doPost(req, resp);
 *         } else if (method.equals("PUT")) {
 *             this.doPut(req, resp);
 *         } else if (method.equals("DELETE")) {
 *             this.doDelete(req, resp);
 *         }
 *     }
 *
 *       1、你的请求过来
 *          http://localhost:8080/owner/query
 *       2、通过 /owner/query 和 OwnerQueryServlet 进行了绑定 (Tomcat 服务器帮我们操作的)
 *          HttpServlet ownerQueryServlet = new OwnerQueryServlet();  //创建对象
 *          //调用方法
 *          ownerQueryServlet.doGet();
 *             指向了 OwnerQueryServlet 的 doGet 的重写方法
 */
@WebServlet("/owner/query")
public class OwnerQueryServlet extends HttpServlet {

    private OwnerService ownerService = new OwnerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //回写的数据设置 编码格式
        resp.setContentType("text/html;charset=utf-8");

        //处理请求
        //查询数据中的数据
        List<Owner> owners = ownerService.listOwner();
        System.out.println("owners = " + owners);

        // 返回数据
        // resp.getWriter().write("返回数据库中查询的数据,数据要是json格式的字符串");

        //使用json框架 可以直接把java对象转成 json 字符串
        String jsonString = JSON.toJSONString(owners);
        resp.getWriter().write(jsonString);
    }

}
